package cn.itcast.wushangxiantu.Activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;

import cn.itcast.wushangxiantu.BaseAdapter.ChouJiangDaoJuGiveViewAdapter;
import cn.itcast.wushangxiantu.BaseAdapter.ChouJiangGiveViewAdapter;
import cn.itcast.wushangxiantu.MessageEvent;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import cn.itcast.wushangxiantu.UserData.UserBag;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ChouJiang extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {

    private GridView gvBag;
    private GridView gvJiangchi;
    private TextView tvSenLin;
    private TextView tvNongYuan;
    private LinearLayout ll;
    private UserBag userBag;
    private ChouJiangDaoJuGiveViewAdapter adapter;
    private ChouJiangGiveViewAdapter adapter2;
    private TextView tvSub;
    private TextView tvAdd;
    public static int number;//倍数
    private TextView tvHand;
    private int startTime;
    private int stopTime;
    private TextView tvJinBi;
    private View viewSenLin;
    private View viewJinBi;
    private View viewNongYuan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chou_jiang);
        //注册
        EventBus.getDefault().register(this);
        initView();
    }

    private void initView() {
        number = 1;
        gvBag = (GridView) findViewById(R.id.gvBag);
        gvJiangchi = (GridView) findViewById(R.id.gvJiangchi);
        tvSenLin = (TextView) findViewById(R.id.tvSenLin);
        tvNongYuan = (TextView) findViewById(R.id.tvNongYuan);
        ll = (LinearLayout) findViewById(R.id.ll);
        getLingshiData(Login.getUser().getUsernumber());
        adapter2 = new ChouJiangGiveViewAdapter(ChouJiang.this);
        gvJiangchi.setAdapter(adapter2);
        tvSub = (TextView) findViewById(R.id.tvSub);
        tvAdd = (TextView) findViewById(R.id.tvAdd);
        tvSub.setOnClickListener(this);
        tvAdd.setOnClickListener(this);
        tvSenLin.setOnClickListener(this);
        tvNongYuan.setOnClickListener(this);
        tvHand = (TextView) findViewById(R.id.tvHand);
        tvJinBi = (TextView) findViewById(R.id.tvJinBi);
        tvAdd.setOnLongClickListener(this);
        tvSub.setOnLongClickListener(this);
        tvJinBi.setOnClickListener(this);


        viewSenLin = (View) findViewById(R.id.viewSenLin);
        viewJinBi = (View) findViewById(R.id.viewJinBi);
        viewNongYuan = (View) findViewById(R.id.viewNongYuan);
    }

    private void getLingshiData(String usernumber) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userName=" + usernumber + "&need=beibao", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("beibao1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("beibao2", src);
                UserBag userBag = null;
                try {
                    userBag = new Gson().fromJson(src, UserBag.class);
                } catch (Exception e) {
                    userBag = null;
                }
                Message message = new Message();
                message.obj = userBag;
                message.what = 1;
                handler.sendMessage(message);
            }
        });
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            switch (message.what) {
                case 1:
                    userBag = (UserBag) message.obj;
                    if (null != userBag) {
                        if (adapter == null) {
                            adapter = new ChouJiangDaoJuGiveViewAdapter(ChouJiang.this);
                        }
                        if (userBag != null) {
                            adapter.setDaojuBean(userBag.getBeibao().get(1));
                            gvBag.setAdapter(adapter);
                        }
                    } else {
                        ToastUtil.toast(ChouJiang.this, "服务器坏掉了");
                    }
                    break;

            }
            return false;
        }
    });

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvSub:
                if (number <= 1) {
                    ToastUtil.toast(ChouJiang.this, "当前以达到最小倍数");
                } else {
                    number--;
                    tvHand.setText(number + "");
                }
                break;
            case R.id.tvAdd:
                if (number >= 10000) {
                    ToastUtil.toast(ChouJiang.this, "当前以达到最大倍数");
                } else {
                    number++;
                    tvHand.setText(number + "");
                }
                break;
            case R.id.tvSenLin:
                if (adapter2.getNum() != 0) {
                    viewSenLin.setVisibility(View.VISIBLE);
                    viewJinBi.setVisibility(View.INVISIBLE);
                    viewNongYuan.setVisibility(View.INVISIBLE);
                    adapter2.setNum(0);
                }
                break;
            case R.id.tvNongYuan:
                if (adapter2.getNum() != 2) {
                    viewNongYuan.setVisibility(View.VISIBLE);
                    viewJinBi.setVisibility(View.INVISIBLE);
                    viewSenLin.setVisibility(View.INVISIBLE);
                    adapter2.setNum(2);
                }
                break;
            case R.id.tvJinBi:
                if (adapter2.getNum() != 1) {
                    viewJinBi.setVisibility(View.VISIBLE);
                    viewNongYuan.setVisibility(View.INVISIBLE);
                    viewSenLin.setVisibility(View.INVISIBLE);
                    adapter2.setNum(1);
                }
                break;
        }
    }

    /**
     * 接收后处理
     *
     * @param messageEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void shuaxing(MessageEvent messageEvent) {
//        if (messageEvent.getMessage() == "DaoJu") {
//            getLingshiData(Login.getUser().getNumber()+"");
//        }
    }

    private void GeneralDialogZahuo(final Context context, String title) {
        /**
         * 编辑框
         * 1标题
         */
        AlertDialog dialog;
        final EditText editText = new EditText(context);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        InputFilter[] filters = {new InputFilter.LengthFilter(9)};
        editText.setFilters(filters);
        final int content = 0;
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(title)
                .setView(editText)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (editText.getText().toString().trim().equals("")) {
                            dialogInterface.dismiss();
                            ToastUtil.toast(context, "输入有误");
                            return;
                        }
                        int num = Integer.parseInt(editText.getText().toString().trim());
                        if (num < 1 || num > 10000) {
                            ToastUtil.toast(context, "数量有误");
                        } else {
                            number = num;
                            tvHand.setText(number + "");
                        }
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
        adapter2.setNum(1);
    }

    @Override
    public boolean onLongClick(View view) {
        switch (view.getId()) {
            case R.id.tvSub:
                GeneralDialogZahuo(ChouJiang.this, "最小值为1");
                break;
            case R.id.tvAdd:
                GeneralDialogZahuo(ChouJiang.this, "最大值为10000");
                break;
        }
        return false;
    }
}